define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
           //alert('88');
        },
        add: function () {
            Controller.api.bindevent();
        },
        sponsorbuydetail: function () {

            $('.jifeizhouqi span:first-child').addClass('mini-span-active');
            $(".mini-span").click(function (){
               $(".mini-span").removeClass('mini-span-active');
               $(this).addClass('mini-span-active');
                $("#jifeiType").val($(this).attr('type'));
                $("#jifeiPrice").val($(this).find('input').val());
                initPrice();
            });
            $('.zheng-add').click(function (){
                var temN = parseInt( $(this).prev('.zheng-number').html())+1;
                $(this).prev('.zheng-number').html(temN);
                var unitNum = temN;
                var unitPrice  = parseFloat(temN*parseInt($(this).parents('.price-num').find('.pUnit').html())).toFixed(2);
                var rongliang = parseInt($(this).parents('.price-num').find('.rongliang').val());
                var xiaojiTotal  = rongliang *unitPrice;
                $(this).closest('.price-num').find('.unitNumber').val(unitNum);
                $(this).closest('.price-num').find('.unitPrice').val(xiaojiTotal);
                $(this).closest('.price-num').find('.zheng-xiaoji-total').html(xiaojiTotal);

                initPrice();
            });
            $('.zheng-reduce').click(function (){
                var temN = parseInt($(this).next('.zheng-number').html())-1;
                var miniNum = $(this).closest('.count-block').find('span .minNum').html(unitPrice);
                if(temN<1){
                    return false;
                }
                $(this).next('.zheng-number').html(temN);
                var unitNum = temN;
                var unitPrice  = parseFloat(temN*parseInt($(this).parents('.price-num').find('.pUnit').html())).toFixed(2);
                var rongliang = parseInt($(this).parents('.price-num').find('.rongliang').val());
                var xiaojiTotal  = rongliang *unitPrice;
                $(this).closest('.price-num').find('.unitNumber').val(unitNum);
                $(this).closest('.price-num').find('.unitPrice').val(xiaojiTotal);
                $(this).closest('.price-num').find('.zheng-xiaoji-total').html(xiaojiTotal);
                initPrice();
            });
            $('#iLiuliangPrice').click(function (){
                if ($(this).prop("checked")) {
                    $(".price-num").show('fast');
                } else {
                    $(".price-num").hide("slow");
                }
                initPrice();
            });
            initPrice();
        },
        order:function() {
            var timer = setInterval(function() {
                $.ajax({
                    url: "/admin/sponsor/sponsorbuy/buylist",
                    type: "post",  // 发送方式
                    dataType: 'JSON',  // 反序列化
                    data:{'orderId':$("#orderId").val()},
                    success: (res) => {
                        if(res.orderStatus==2){
                            clearInterval(timer);
                            location.href = '{$orderData.returnurl}';
                        }
                    },
                    error:()=>{  // 发送失败的回调函数
                        //console.log("失败");
                    }
                });
            }, 1500);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
function initPrice (){
    var thisType = $('#type').val();
    switch (thisType) {
        case '1':
        case '2':
        case '3':
        case '4':
              var tPrice = $('#jifeiPrice').val();
              if ($('#iLiuliangPrice').prop("checked")) {
                  tPrice = parseInt(tPrice) + parseInt($('.unitPrice').val());
              }
            var resultPrice  = parseFloat(tPrice).toFixed(2);
              $('.total-fee').html(resultPrice);
              $('#totalFee').val(resultPrice);
              break;
        case '6':
            var tPrice = $('.xkzPrice').val();
            $("input[name^='vRoomPrice']").each(function (){
                tPrice = parseInt(tPrice) + parseInt($(this).val());
            });
            //按月季费
            var totalMonth = $(".mini-span-active").find('input').val();
            tPrice *= totalMonth;
            var resultPrice  = parseFloat(tPrice).toFixed(2);
            $('.total-fee').html(resultPrice);
            $('#totalFee').val(resultPrice);
            break;
        case '7':
            var tPrice = $('.xkzPrice').val();
            $("input[name^='vRoomPrice']").each(function (){
                tPrice = parseFloat(tPrice) + parseFloat($(this).val());
            });
            var totalMonth = $(".mini-span-active").find('input').val();
            tPrice *= totalMonth;
            var resultPrice  = parseFloat(tPrice).toFixed(2);
            $('.total-fee').html(resultPrice);
            $('#totalFee').val(resultPrice);
            break;
    }
}
